﻿/******************************************************************************
* File description: StatMaintBarCode.qml
* Function: 统计保养表格
* Current Version: 1.1
* Author: 陈乃瑞
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.0
import "../components"

Rectangle {
    id: root

    height: parent.height; width: parent.width

    property var tableData: [[1,2,3,4,5,6],[1,2,3,4,5,6]]


    signal updateCodeNum(int num)

    function refreshTableData(){
        tableData = []
        var codeList = CBarCode.getBarCodeList()
        for(let i = 0; i < codeList.length; i ++){
            tableData.push([])
            tableData[i].push(i+1)
            tableData[i].push(codeList[i]["CreateDate"])
            tableData[i].push(codeList[i]["Code"])
//                tableData[i].push(codeList[i]["State"])
            tableData[i].push(codeList[i]["Times"]>0 ? "有效" : "无效")
            tableData[i].push(codeList[i]["Times"])
            tableData[i].push("")
        }
        tableDataChanged()
        updateCodeNum(CBarCode.getBarCodeNum())
    }

    onVisibleChanged: {
        if(visible){
            refreshTableData()
        }
    }

    property real lineHeight: 50*dp

    radius: 5*dp

    color: "#F7F7F7"

    Text{
        x: 20*dp; y: 20*dp
        text: "耗材编码统计"
        font.pixelSize: config.fontsize + 3*dp
        font.bold: true
    }

    Row{
        height: 40*dp
        width: 300*dp

    }

    Rectangle{
        x: 20*dp; y: 70*dp
        height: parent.height - 90*dp; width: parent.width - 2*x
        // 这是一个表格
        Item{
            id: table
            property var title: ["序号", "录入时间", "耗材编码", "状态", "剩余次数", "操作"]
            property var tableScale: [0.15, 0.2, 0.2, 0.15, 0.15, 0.15]
            x: 20*dp; y: 20*dp
            height: parent.height - 2*y; width: parent.width - 2*x
            // 表身
            ListView{
                y: lineHeight
                height: parent.height - lineHeight; width: table.width
                clip: true
                model: tableData.length
                delegate: Rectangle{        // 表行
                    id: lineItem
                    height: lineHeight; width: table.width
                    property int lineIdx: index
                    Row{
                        height: lineHeight; width: table.width
                        Repeater{
                            model: tableData[lineItem.lineIdx].length
                            Item{
                                height: lineHeight; width: table.tableScale[index] * table.width
                                Text{
                                    anchors.centerIn: parent
                                    text: tableData[lineItem.lineIdx][index]
                                    font.pixelSize: config.fontsize
                                    color: {
                                        if(text === "有效") return "green"
                                        if(text === "无效") return "red"
                                        return "#222"
                                    }
                                }
                                CustomImage{
                                    visible: index === 5
                                    anchors.centerIn: parent
                                    height: 25*dp; width: 25*dp
                                    source: "qrc:/picture/pz_sc.png"
                                    MouseArea{
                                        anchors.fill: parent
                                        cursorShape: Qt.PointingHandCursor
                                        onClicked: {
                                            CBarCode.deleteBarCode(tableData[lineItem.lineIdx][2])
                                            refreshTableData()
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Rectangle{
                        height: 1; width: parent.width
                        color: "#EBEDF0"
                        y: parent.height - height
                    }
                }
            }

            // 表头
            Rectangle{
                height: lineHeight; width: parent.width
                color: "#F7F8FA"
                border.color: "#EBEDF0"
                Row{
                    anchors.fill: parent
                    Repeater{
                        model: table.title.length
                        anchors.fill: parent
                        Item{
                            height: lineHeight; width: parent.width * table.tableScale[index]
                            Text{
                                anchors.centerIn: parent
                                text: table.title[index]
                                font.pixelSize: config.fontsize
                                font.bold: true
                            }
                        }
                    }
                }
            }
        }
    }

}
